package com.expressage.controller;

import com.expressage.po.Board;
import com.expressage.service.BoardService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * @author Gt
 * 公告控制器
 */
@Controller
public class BoardController {

    Logger logger = Logger.getLogger(BoardController.class);

    @Autowired
    BoardService boardService;

/*    @RequestMapping("/toboardlist")
    public String tolist(){
        return "jspbefore/";
    }*/

    //查询所有公告
    @RequestMapping("/board")
    public String list(Model model){
        List<Board> list = boardService.findAll();
        model.addAttribute("list",list);
        logger.info(list);
        return "jspbefore/board";
    }

    @RequestMapping("/boardlist")
    public String list1(Model model){
        List<Board> list = boardService.findAll();
        model.addAttribute("list",list);
        logger.info(list);
        return "jspafter/boardlist";
    }


    //公告详情查询
    @RequestMapping("/boardinfo")
    public String boardInfo(int bid,Model model){
        logger.info("进入帖子详情");
        logger.debug(bid);
        Board board = boardService.findBybid(bid);
        logger.debug(board);
        logger.debug(board.getTitle());
        model.addAttribute("board",board);
        return "jspbefore/boardinfo";
    }

    @RequestMapping("/toaddboard")
    public String toAdd(){
        return "jspafter/addboard";
    }

    @RequestMapping("/addboard")
    public String addboard(Board board){
        boolean flag = boardService.insert(board);
        logger.info(flag ? "添加公告成功" : "添加公告失败");
        //重定向到帖子列表控制器
        return "redirect:boardlist.action";
    }

    @RequestMapping("/deleteboard")
    public String deleteBoard(int[] bid){
        logger.info(bid);
        if(bid.length>0){
            boolean flag = boardService.delete(bid);
            logger.info(flag ? "公告删除成功":"公告删除失败");
        }
        return "redirect:boardlist.action";
    }



}
